var page = undefined;
var address = 'http://user.cross.echosite.cn';

Page({
  data: {
    cardCur: 0,
    swiperList: [{
      id: 0,
      type: 'image',
      url: address + '/file/Resource_1.jpg'
    }, {
      id: 1,
        type: 'image',
        url: address + '/file/Resource_2.jpg'
    }, {
      id: 2,
      type: 'image',
      url: address + '/file/Resource_3.jpg'
    }, {
      id: 3,
      type: 'image',
      url: address + '/file/Resource_2.jpg'
    }, {
      id: 4,
      type: 'image',
      url: address + '/file/Resource_5.jpg'
    }, {
      id: 5,
      type: 'image',
      url: address + '/file/Resource_1.jpg'
    }, {
      id: 6,
      type: 'image',
      url: address + '/file/Resource_3.jpg'
    },
    ],


//////////////////////////
    imgArr: [
      { src: address + '/file/Resource_1.jpg', isturn: false, zIndex: 4 },
      { src: address + '/file/Resource_2.jpg', isturn: false, zIndex: 3 },
      { src: address + '/file/Resource_3.jpg', isturn: false, zIndex: 1 },
      { src: address + '/file/Resource_4.jpg', isturn: false, zIndex: 1 },
      { src: address + '/file/Resource_5.jpg', isturn: false, zIndex: 1 },
      { src: address + '/file/Resource_6.jpg', isturn: false, zIndex: 1 },
      { src: address + '/file/Resource_7.jpg', isturn: false, zIndex: 1 }
    ],
    flag:true,

    /////////////// set date
    doommData:[]
  },
  onLoad() {
    this.towerSwiper('swiperList');
    // 初始化towerSwiper 传已有的数组名即可

    page = this;
  },
  onShareAppMessage() {
    return {
      title: "生日快乐呀❤2020.05.21",
      imageUrl: "/common/images/share.jpg",
    };
  },


  bindbt:function(){
    var arr = ["我爱你","生日快乐","Happy Birthday", "天天爱你","余生请你多多指教","做我女朋友吧" ];
    var str = arr[Math.floor((Math.random()*arr.length))];

    doommList.push(new Doomm(str,Math.ceil(Math.random()*100),Math.ceil(Math.random()*10),getRandomColor()));
    this.setData({
      doommData : doommList
    })
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },


  /////////////////////////
  setclass: function (e) {
    if(this.data.flag){
      this.data.flag=false;
      let imgs = this.data.imgArr;

      let index = e.currentTarget.dataset.index;
      imgs.map((ele,i)=>{
        if (index == i) {
          imgs[i].isturn = !imgs[i].isturn;
          imgs[i].zIndex = 4;

        } else {
          imgs[i].zIndex = 1;
        }
      });
      if (index - 1 >= 0) {
        imgs[index - 1].zIndex = 3;
      }
      if (index + 1 < imgs.length) {
        imgs[index + 1].zIndex = 3;
      }
      if (index - 2 >= 0) {
        imgs[index - 2].zIndex = 2;
      }
      if (index + 2 < imgs.length) {
        imgs[index + 2].zIndex = 2;
      }
      this.setData({
        imgArr: imgs,
      })
    }



  },
  start(){
    console.log('动画开始')
  },
  end(e){
    console.log(1111);
    this.data.flag=true;
    this.bindbt()
  },
});












var doommList=[];
var i=0;
class Doomm{
  constructor(text,top,time,color){
    this.text = text+i;
    this.top = top;
    this.time = time;
    this.color = color;
    this.display = true;
    let that = this;
    this.id = i++;
    setTimeout(function(){
      doommList.splice(doommList.indexOf(that),1);
      page.setData({
        doommData : doommList
      })
    },this.time*1000)
  }
}
function getRandomColor() {
  let rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}